﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace AChance.Func
{
    public static class FunFiles
    {
        public static string CurrentPath { get; } = FunContext.CurrentPath + "Files\\";
        public static bool Init()
        {
            try
            {
                if (!Directory.Exists(CurrentPath))
                    Directory.CreateDirectory(CurrentPath);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static void Clear()
        {
            try
            {
                if (!Directory.Exists(CurrentPath))
                    return;
                DirectoryInfo filesPath = new DirectoryInfo(CurrentPath);
                foreach (FileInfo filesFile in filesPath.GetFiles())
                {
                    filesFile.Delete();
                }
            }
            catch { }
        }
        public static void ClearExpired()
        {
            try
            {
                if (!Directory.Exists(CurrentPath))
                    return;
                DirectoryInfo filesPath = new DirectoryInfo(CurrentPath);
                foreach (FileInfo filesFile in filesPath.GetFiles())
                {
                    if (filesFile.LastWriteTime > DateTime.Now.AddDays(-10))
                        continue;
                    filesFile.Delete();
                }
            }
            catch { }
        }
        public static bool Exists(string fileName)
        {
            return File.Exists(CurrentPath + fileName);
        }
        public static bool Read(string fileName, out byte[] fileContent)
        {
            fileContent = null;
            try
            {
                fileContent = File.ReadAllBytes(CurrentPath + fileName);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool Write(string fileName, Stream fileContent)
        {
            try
            {
                Byte[] bytFileContent = new byte[fileContent.Length];
                fileContent.Seek(0, SeekOrigin.Begin);
                fileContent.Read(bytFileContent, 0, (int)fileContent.Length);
                fileContent.Seek(0, SeekOrigin.Begin);
                return Write(fileName, bytFileContent);
            }
            catch
            {
                return false;
            }
        }
        public static bool Write(string fileName, byte[] fileContent)
        {
            try
            {
                File.WriteAllBytes(CurrentPath + fileName, fileContent);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static void Delete(string fileName)
        {
            try
            {
                if (!Directory.Exists(CurrentPath))
                    return;
                File.Delete(CurrentPath + fileName);
            }
            catch { }
        }
    }
}
